import os
from datetime import date, datetime
import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5 import QtWidgets, QtGui


__author__ = 'chb'


MONTH = 6
DAY = 7
HOUR = 0


def get_exam_time():
	now = datetime.now()
	year = now.year
	exam = datetime(year, MONTH, DAY, HOUR)

	if now > exam:
		year += 1
		
	return datetime(year, MONTH, DAY, HOUR)


EXAM_DATETIME = get_exam_time()
print('exam = %s' % EXAM_DATETIME)


def read_pos():
	if not os.path.exists('C:\\.pos'):
		return 687, 12

	with open('C:\\.pos') as f:
		ln = f.readline()
		try:
			x, y = ln.split(' ')
			x, y = int(x), int(y)
			return x, y
		except:
			return 687, 12


def save_pos(x, y):
	with open('C:\\.pos', 'w+') as f:
		f.write('%s %s' % (x, y))


class Timer(QWidget):
	def __init__(self, parent=None, **kwargs):
		super().__init__(parent)
		self.setWindowFlags(Qt.FramelessWindowHint|Qt.WindowStaysOnBottomHint|Qt.SubWindow)
		self.setAutoFillBackground(False)
		self.setAttribute(Qt.WA_TranslucentBackground, True)
		self.repaint()
		
		quit_action = QAction('退出', self, triggered=self.quit)
		self.tray_icon_menu = QMenu(self)
		self.tray_icon_menu.setIcon(QIcon('./shime1.jpg'))
		self.tray_icon_menu.addAction(quit_action)
		self.tray_icon = QSystemTrayIcon(self)
		self.tray_icon.setIcon(QIcon('./shime1.jpg'))
		self.tray_icon.setContextMenu(self.tray_icon_menu)
		self.tray_icon.show()

		self.setContextMenuPolicy(Qt.CustomContextMenu)
		self.customContextMenuRequested.connect(self._show_menu)
		
		self.label = QLabel(self)
		self.label.setText('Hello world')
		self.label.setAlignment(Qt.AlignCenter)
		self.label.setStyleSheet('color:white')
		
		font = QFont()
		font.setPointSize(50)
		font.setFamily('Microsoft YaHei')
		font.setBold(True)
		self.label.setFont(font)

		box = QVBoxLayout()
		box.addWidget(self.label)

		self.is_follow_mouse = False
		self.mouse_drag_pos = self.pos()
		self.resize(512, 128)
		self.setLayout(box)

		self.timer = QTimer()
		self.timer.timeout.connect(self.start_countdown)
		self.timer.start(10)

		self.timer2 = QTimer()
		self.timer2.timeout.connect(self.report_pos)
		self.timer2.start(500)
		
		x, y = read_pos()
		self.move(x, y)
		self.show()

	def _show_menu(self, p):
		dest_point=self.mapToGlobal(p)
		self.tray_icon_menu.exec_(dest_point)

	def report_pos(self):
		print(self.x(), self.y())
		save_pos(self.x(), self.y())

	def start_countdown(self):
		delta = EXAM_DATETIME - datetime.now()
		self.label.setText(
			'距离%s年高考仅剩\n%s' % (
				EXAM_DATETIME.year, 
				str(delta)[:-4])
			)

	def mousePressEvent(self, event):
		if event.button() == Qt.LeftButton:
			self.is_follow_mouse = True
			self.mouse_drag_pos = event.globalPos() - self.pos()
			event.accept()
			self.setCursor(QCursor(Qt.OpenHandCursor))
			
	def mouseMoveEvent(self, event):
		if Qt.LeftButton and self.is_follow_mouse:
			self.move(event.globalPos() - self.mouse_drag_pos)
			event.accept()
			
	def mouseReleaseEvent(self, event):
		self.is_follow_mouse = False
		self.setCursor(QCursor(Qt.ArrowCursor))
		
	def quit(self):
		self.close()
		sys.exit()


def main():
	app = QApplication(sys.argv)
	_ = Timer()
	sys.exit(app.exec_())


if __name__ == '__main__':
	main()